﻿/******************************************************************************
 * 【本类功能概述】                                 					      *
 *  版权所有（C）2017-20XX，                                        *
 *  保留所有权利。                                                            *
 ******************************************************************************
 *  作者 : <hughjk>
 *  创建时间:2019/11/24 21:39:48
 *  文件描述:控制器
 *****************************************************************************/

using Ability;
using StateModel;
using UnityEngine;

public enum ControllerType
{
    Barracks,//兵营
    Soldier,//士兵
    SCV
}

public class Controller : MonoBehaviour
{
    [HideInInspector] public AbilityManage m_AbilityManage;
    [HideInInspector] public Instruct m_input;
    [HideInInspector] public Transform m_transform;
    [HideInInspector] public Rigidbody2D m_rig;
    [HideInInspector] public StateManage m_StateManage;
    private ModelInfo[] _model;
    public ControllerType _Type;
    public TeamType _TeamType;

    public bool isAuto = false;
    public bool isInit = false;

    [Header("属性")]
    public string _Name;

    public Sprite _Icon;
    public int _Id;
    public float _HP = 100;
    public float _MaxHP = 100;
    public float _DrillTime = 3;//创造时间

    private void Awake()
    {
        if (isAuto) {
            Init();
        }
    }

    public void Init()
    {
        if (isInit) {
            return;
        }
        isInit = true;
        m_transform = transform;
        m_rig = m_transform.GetComponent<Rigidbody2D>();
        m_input = m_transform.GetComponent<Instruct>();
        m_AbilityManage = m_transform.GetComponent<AbilityManage>();
        m_StateManage = m_transform.GetComponent<StateManage>();

        _model = m_transform.GetComponents<ModelInfo>();

        InitModle();
        GetModel();
    }

    public virtual void InitData()
    {
        _HP = _MaxHP;

        for (int i = 0; i < _model.Length; i++) {
            _model[i].InitData(this);
        }
    }

    public virtual void InitModle()
    {
        for (int i = 0; i < _model.Length; i++) {
            _model[i].InitModel(this);
        }
    }

    public virtual void GetModel()
    {
        for (int i = 0; i < _model.Length; i++) {
            _model[i].GetModel();
        }
    }

    public virtual void Update()
    {
        if (!isInit) {
            return;
        }
        float dt = Time.deltaTime;
        m_input.OnUpdata(dt);
        m_AbilityManage.OnUpdata(dt);
        m_StateManage.OnUpdata(dt);

        if (_HP <= 0) {
            GameObject.DestroyImmediate(gameObject);
        }
    }
}